package com.jvtd.eyl.ui.main.home;

import com.jvtd.eyl.base.BasePresenter;
import com.jvtd.eyl.bean.binding.ClassBean;
import com.jvtd.eyl.bean.binding.PeriodicalBean;
import com.jvtd.eyl.bean.http.BannerBean;
import com.jvtd.eyl.data.DbManager;
import com.jvtd.eyl.rxjava.JvtdRxSchedulers;
import com.jvtd.rxjava.JvtdObserverSubscriber;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;

/**
 * 精选
 *
 * 作者:chenlei
 * 时间:2018/11/12 3:21 PM
 */
public class HomePresenter<V extends HomeMvpView> extends BasePresenter<V>
        implements HomeMvpPresenter<V> {

    @Inject
    public HomePresenter(DbManager dbManager, CompositeDisposable disposable) {
        super(dbManager, disposable);
    }

    @Override
    public void getBannerList() {
        if (!isAttachView()) return;
        getCompositeDisposable().add(getDbManager().getBannerList()
                .compose(JvtdRxSchedulers.handleObservableResult())
                .subscribeWith(new JvtdObserverSubscriber<List<BannerBean>>(getMvpView()) {
                    @Override
                    public void onNext(@NonNull List<BannerBean> list) {
                        super.onNext(list);
                        if (isAttachView())
                            getMvpView().getBannerListSuccess(list);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                    }
                }));
    }

    @Override
    public void getClassList() {
        if (!isAttachView()) return;
        getCompositeDisposable().add(getDbManager().getClassList()
                .compose(JvtdRxSchedulers.handleObservableResult())
                .subscribeWith(new JvtdObserverSubscriber<List<ClassBean>>(getMvpView()) {
                    @Override
                    public void onNext(List<ClassBean> classBeans) {
                        super.onNext(classBeans);
                        if (isAttachView()) {
                            getMvpView().getClassListSuccess(classBeans);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (isAttachView())
                            getMvpView().getClassListFailed();
                    }
                }));
    }

    @Override
    public void getPeriodicalList() {
        if (!isAttachView()) return;
        getCompositeDisposable().add(getDbManager().getAllPeriodicalList()
                .compose(JvtdRxSchedulers.handleObservableResult())
                .subscribeWith(new JvtdObserverSubscriber<List<PeriodicalBean>>(getMvpView()) {
                    @Override
                    public void onNext(List<PeriodicalBean> periodicalBeans) {
                        super.onNext(periodicalBeans);
                        if (isAttachView()) {
                            getMvpView().getPeriodicalListSuccess(periodicalBeans);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (isAttachView()) {
                            getMvpView().getPeriodicalListFailed();
                        }
                    }
                }));
    }
}
